package com.solo.dao;

import com.solo.entity.Article;
import com.solo.utils.DBUtil;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
// import java.util.Date;
import java.util.Date;
import java.util.List;

public class ArticleDAO {
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;

    /**
     * 新增文章
     * @param article
     * @return
     */
    public boolean addArticle(Article article) {
        boolean state = false;
        conn = DBUtil.getConn();
        try {
            // String sql = "insert into `sys_user`(username,password,name,email,phonenumber,gender) values(?, ?, ?, ?, ?, ?)";
            String sql = "insert into `blog_article`(title,content) values(?, ?)";
            pst = conn.prepareStatement(sql);
            pst.setString(1, article.getTitle());
            pst.setString(2, article.getContent());

            int index = pst.executeUpdate();
            System.out.println("action====");
            if (index > 0) {
                state = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(rs, pst, conn);
        }
        return state;
    }

    /**
     * 查询所有文章
     *
     * @return
     * @throws Exception
     */
    public List<Article> selectAllArticle() {
        conn = DBUtil.getConn();
        List<Article> listArticle = new ArrayList<>();
        try {
            String sql = "SELECT * FROM blog_article";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            // 也可以使用createStatement()
            // Statement statement = conn.createStatement();
            // ResultSet resultSet = statement.executeQuery(sql);

            while (rs.next()) {
                int id = rs.getInt("id");
                String title = rs.getString("title");
                String content = rs.getString("content");
                Long categoryId = rs.getLong("category_id");
                Long userId  = rs.getLong("user_id");
                Date createdTime = rs.getDate("created_time");
                // 调用构造方法实例化User对象
                Article Article = new Article(id, title, content,categoryId,userId,createdTime);
                listArticle.add(Article);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(rs, pst, conn);
        }
        return listArticle;
    }

    /**
     * 删除文章
     * @param article
     * @return
     */
    public boolean deleteArticle(Article article) {
        boolean rowDeleted = false;
        conn = DBUtil.getConn();
        try {
            String sql = "DELETE FROM blog_article where id = ?";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setLong(1, article.getId());

            rowDeleted = statement.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(rs, pst, conn);
        }
        return rowDeleted;
    }

    /**
     * 更新文章信息
     * @param article
     * @return
     */
    public boolean updateArticle(Article article) {
        boolean rowUpdated = false;
        conn = DBUtil.getConn();

        try {
            String sql = "UPDATE blog_article SET title = ?, content = ?, category_id = ?, user_id = ?, views = ?, created_time = ?, update_time = ?";
            sql += " WHERE id = ?";
            pst = conn.prepareStatement(sql);
            Date sqlDate = article.getCreatedTime();
            Date utilDate = new Date(sqlDate.getTime()); // 转换为 java.util.Date
            pst.setString(1, article.getTitle());
            pst.setString(2, article.getContent());
            pst.setLong(3,article.getCategoryId());
            pst.setLong(4,article.getUserId());
            pst.setInt(5,article.getViews());
            pst.setDate(6,new java.sql.Date(article.getCreatedTime().getTime()));
            pst.setLong(7, article.getId());

            rowUpdated = pst.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(rs, pst, conn);
        }
        return rowUpdated;
    }

    /**
     * 按id号查询文章
     * @param id
     * @return
     */
    public Article selectArticleById(Long id) {
        conn = DBUtil.getConn();
        Article article = null;
        try {
            String sql = "SELECT * FROM blog_article WHERE id = ?";
            pst = conn.prepareStatement(sql);
            pst.setLong(1, id);
            rs = pst.executeQuery();
            if (rs.next()) {
                String title = rs.getString("title");
                String content = rs.getString("content");
                Long categoryId = rs.getLong("category_id");
                Long userId  = rs.getLong("user_id");
                Date createdTime = rs.getDate("created_time");
                // 调用构造方法实例化User对象
                article = new Article(id, title, content,categoryId,userId,createdTime);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConn(rs, pst, conn);
        }

        return article;
    }
}
